package com.wqssyq.copychinese;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class CView extends ImageView{
	private static String LOG_TAG = "CView";
	private static boolean isFirst = true;
	
    private Paint  mPaint;
    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;

	public CView(Context context) {
		super(context);

//		Log.i(LOG_TAG, "CView");
//		try {
//			//Thread.dumpStack();
//			new RuntimeException("dumpStack");
//		} catch (Exception e) {
//			//e.printStackTrace(err)
//			//e.printStackTrace(System.out);
//			//PrintWriter
//			//java.io.BufferedWriter bw = new java.io.BufferedWriter();
////			ByteArrayOutputStream bos = new ByteArrayOutputStream();
////			PrintWriter pw = new PrintWriter(bos);
////			e.printStackTrace(pw);
//			Log.e(LOG_TAG, "error", e);
//		}

	}
    public CView(Context context, AttributeSet attrs) {
    	super(context, attrs);
    }
    public CView(Context context, AttributeSet attrs, int defStyle) {
    	super(context, attrs, defStyle);
    }
    
    private int min(int a, int b){
    	return b>a ? a : b;
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    
    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
    }
    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }

    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
        }
        return true;
    }
    
    
    public void doFirst(){
    	int height;
    	int width;
    	int textSize;
    	
        height = this.getHeight();
        width = this.getWidth();
        mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);

        int left = getLeft();
        int right = getRight();
        int top = getTop();
        int bottom = getBottom();
        int margin = 10;
        //textSize = height>width ? width : height;
        textSize = min(width-2*margin, height-2*margin);
        
//        Log.i(LOG_TAG, "left:" + left);
//        Log.i(LOG_TAG, "right:" + right);
//        Log.i(LOG_TAG, "top:" + top);
//        Log.i(LOG_TAG, "bottom:" + bottom);

        
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setColor(Color.RED);
        p.setTextSize( textSize );
//		Log.i(LOG_TAG, "TextData.getIdxData():" + TextData.getIdxData());
        String text = TextData.getData();
//        Log.i(LOG_TAG, "text:" + text);
        mCanvas.drawText( text, margin, textSize, p);
        
        Log.i(LOG_TAG, "doFirst:80");
        if(mBitmapPaint==null){
        	mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        //mPaint.setColor(0xFFFF0000);
        mPaint.setColor(0xFF00FF00);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);
		

//        p.setTextSize( 10 );
//        
//    	for(int i=20; i<430; i += 20){
//    		canvas.drawText( i+"", 20, i, p);
//    	}
        
        //return mBitmap;
    	this.setImageBitmap(mBitmap);
    }    
    
    @Override
    protected void onDraw(Canvas canvas) {
    	//Log.i(LOG_TAG, "onDraw");
    	canvas.drawColor(0xFFAAAAAA);
    	
//    	Log.i(LOG_TAG, "onDraw:isFirst:" + isFirst);
    	if(mBitmap==null || mBitmapPaint==null){
//        	Log.i(LOG_TAG, "isFirst:" + isFirst);
//        	Log.i(LOG_TAG, "mBitmap:" + mBitmap);
//        	Log.i(LOG_TAG, "mBitmapPaint:" + mBitmapPaint);
//        	Log.i(LOG_TAG, "TextData.getIdxData():" + TextData.getIdxData());
    		isFirst = true;
        }
    	if(isFirst){
    		doFirst();
    		isFirst = false;
    	}
		canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        
        canvas.drawPath(mPath, mPaint);
    	
    }	
}
